<?php
defined('BY_JYA') or exit('error');
list($params)=$arguments;
		$card_type = $params['card_type'];
		$logo_url = $params['wxlogourl'];	
		$brand_name = $params['brand_name']?:'商铺名称';
		$code_type = 'CODE_TYPE_NONE';
		$title = $params['title'];
		//$color = $params['color'];
		//if (empty($color)) {
			$color = 'Color010';
		//}
		$notice = $params['notice'];
		$service_phone = $params['service_phone'];
		$description = $params['description'];
		$type = $params['datetype'];
		if ($type == '1') {
			$begin_timestamp = $params['begin_timestamp'];
			$end_timestamp = $params['end_timestamp'];
		}
		else {
			if ($type == '0') {
				$fixed_term = (empty($params['fixed_term']) ? 0 : $params['fixed_term']);
				$fixed_begin_term = (empty($params['fixed_begin_term']) ? 0 : $params['fixed_begin_term']);
			}
		}
		$quantity = (empty($params['quantity']) ? 100 : $params['quantity']);
		$use_limit = (empty($params['use_limit']) ? 1 : $params['use_limit']);
		$get_limit = (empty($params['get_limit']) ? 1 : $params['get_limit']);
		$use_custom_code = false;
		$bind_openid = false;
		$can_share = (empty($params['can_share']) ? false : true);
		$can_give_friend = (empty($params['can_give_friend']) ? false : true);
		$location_id_list = '';
		$center_title = $params['center_title'];
		$center_sub_title = $params['center_sub_title'];
		if($_SESSION['wxapp_id']){
			if (strpos($params['center_url'], 'http://')) {
				$center_url = str_replace('http://', '', $params['center_url']);
			}
			if (strpos($params['center_url'], 'https://')) {
				$center_url = str_replace('https://', '', $params['center_url']);
			}
		}else{
			$center_url='';
		}
		
		//$center_url = $this->checkurl($params['center_url']);
		$setcustom = $params['setcustom'];
		if (!empty($setcustom)) {
			$custom_url_name = $params['custom_url_name'];
			$custom_url_sub_title = $params['custom_url_sub_title'];
			if($_SESSION['wxapp_id']){
				if (strpos($params['custom_url'], 'http://')) {
					$custom_url = str_replace('http://', '', $params['custom_url']);
				}
				if (strpos($params['custom_url'], 'https://')) {
					$custom_url = str_replace('https://', '', $params['custom_url']);
				}
			}
				//$custom_url = $this->checkurl($params['custom_url']);
		}
		$setpromotion = $params['setpromotion'];
		if (!empty($setpromotion)) {
			$promotion_url_name = $params['promotion_url_name'];
			$promotion_url_sub_title = $params['promotion_url_sub_title'];
			//$promotion_url = $this->checkurl($params['promotion_url']);
			if($_SESSION['wxapp_id']){
				if (strpos($params['promotion_url'], 'http://')) {
					$promotion_url = str_replace('http://', '', $params['promotion_url']);
				}
				if (strpos($params['promotion_url'], 'https://')) {
					$promotion_url = str_replace('https://', '', $params['promotion_url']);
				}
			}
		}
		$source = '';
		$can_use_with_other_discount = (empty($params['can_use_with_other_discount']) ? false : true);
		$setabstract = $params['abstractimglocalpath'];
		$abstract = $params['abstract'];
		$icon_url_list = $params['icon_url_list'];
		$text_image_list = $params['text_image_list'];
		$time_limit = '';
		$business_service = '';
		$business = array();
		if (!empty($params['BIZ_SERVICE_FREE_WIFI'])) {
			$business[] = '"BIZ_SERVICE_FREE_WIFI"';
		}
		if (!empty($params['BIZ_SERVICE_WITH_PET'])) {
			$business[] = '"BIZ_SERVICE_WITH_PET"';
		}
		if (!empty($params['BIZ_SERVICE_FREE_PARK'])) {
			$business[] = '"BIZ_SERVICE_FREE_PARK"';
		}
		if (!empty($params['BIZ_SERVICE_DELIVER'])) {
			$business[] = '"BIZ_SERVICE_DELIVER"';
		}
		if (!empty($business)) {
			$business_service = implode(',', $business);
		}
		if ($card_type == 'CASH') {
			$accept_category = $params['accept_category'];
			$reject_category = $params['reject_category'];
			$least_cost = (empty($params['least_cost']) ? 0 : $params['least_cost']);
			$reduce_cost = (empty($params['reduce_cost']) ? 0 : $params['reduce_cost']);
		}else if ($card_type == 'DISCOUNT') {
			$discount = (empty($params['discount']) ? 0 : $params['discount']);
		}else if ($card_type == 'MEMBER_CARD') {
			$background_pic_url = $params['background_pic_url'];
			$supply_bonus = $params['supply_bonus'];
			$supply_balance = $params['supply_balance'];
			$bonus_url = $params['bonus_url'];
			$balance_url = $params['balance_url'];
			$prerogative = $params['prerogative'];
			$auto_activate = $params['auto_activate'];
			$activate_url = $params['activate_url'];
			$custom_field1 = $params['custom_field1'];
			if (!empty($custom_field1)) {
				$custom_field_name_type1 = $params['custom_field_name_type1'];
				$custom_field_url1 = $params['custom_field_url1'];
			}
			$custom_field2 = $params['custom_field2'];
			if (!empty($custom_field2)) {
				$custom_field_name_type2 = $params['custom_field_name_type2'];
				$custom_field_url2 = $params['custom_field_url2'];
			}
			$custom_field3 = $params['custom_field3'];
			if (!empty($custom_field3)) {
				$custom_field_name_type3 = $params['custom_field_name_type3'];
				$custom_field_url3 = $params['custom_field_url3'];
			}
			$custom_cell1 = $params['custom_cell1'];
			if (!empty($custom_cell1)) {
				$custom_cell1_name = $params['custom_cell1_name'];
				$custom_cell1_tips = $params['custom_cell1_tips'];
				$custom_cell1_url = $params['custom_cell1_url'];
			}
		}else {
			return false;
		}
		if($type == '1') {
			$date_info=array(
				"type"=>"DATE_TYPE_FIX_TIME_RANGE",
				"begin_timestamp"=>$begin_timestamp,
				"end_timestamp"=>$end_timestamp,
			);
		}else if ($type == '0') {
			$date_info=array(
				"type"=>"DATE_TYPE_FIX_TERM",
				"fixed_term"=>$fixed_term,
				"fixed_begin_term"=>$fixed_begin_term,
			);
		}else if($type == 'DATE_TYPE_PERMANENT') {
			$date_info=array(
				"type"=>"DATE_TYPE_PERMANENT",
			);
		}
		$base_info=array(
			'logo_url'=>$logo_url,
			'brand_name'=>$brand_name,
			'code_type'=>$code_type,
			'title'=>$title,
			'color'=>$color,
			'notice'=>$notice,
			'service_phone'=>$service_phone,
			'description'=>$description,
			'date_info'=>$date_info,
			'sku'=>array('quantity'=>$quantity),
			'use_limit'=>$use_limit,
			'get_limit'=>$get_limit,
			'use_custom_code'=>$use_custom_code,
			'bind_openid'=>$bind_openid,
			'can_share'=>$can_share,
			'can_give_friend'=>$can_give_friend,
			'location_id_list'=>$location_id_list,
			'center_title'=>$center_title,
			'center_sub_title'=>$center_sub_title,
			//'center_url'=>$center_url,
			'custom_url_name'=>$custom_url_name,
			//'custom_url'=>$custom_url,
			'custom_url_sub_title'=>$custom_url_sub_title,
			'promotion_url_name'=>$promotion_url_name,
			//'promotion_url'=>$promotion_url,
			'source'=>$source,
		);
		$use_condition['can_use_with_other_discount']=$can_use_with_other_discount;
		if (!empty($accept_category)) {
			$use_condition['accept_category']=$accept_category;
		}
		if (!empty($reject_category)) {
			$use_condition['reject_category']=$reject_category;
		}
		$advanced_info=array(
			'use_condition'=>$use_condition,
			'abstract'=>array(
				'abstract'=>$abstract,
				'icon_url_list'=>$icon_url_list,
			),
			'text_image_list'=>$text_image_list,
			'business_service'=>$business_service,
		);
		$card['card_type']=$card_type;
		if($card_type == 'GROUPON'){
			$card['groupon']=array(
				'base_info'=>$base_info,
				'advanced_info'=>$advanced_info,
				'deal_detail'=>$deal_detail,
			);
		}elseif($card_type == 'CASH'){
			$card['cash']=array(
				'base_info'=>$base_info,
				'advanced_info'=>$advanced_info,
				'least_cost'=>$least_cost,
				'reduce_cost'=>$reduce_cost,
			);
		}else if ($card_type == 'DISCOUNT') {
			$card['discount']=array(
				'base_info'=>$base_info,
				'advanced_info'=>$advanced_info,
				'discount'=>$discount,
			);
		}else if ($card_type == 'GIFT') {
			$card['gift']=array(
				'base_info'=>$base_info,
				'advanced_info'=>$advanced_info,
				'gift'=>$gift,
			);
		}else if ($card_type == 'GENERAL_COUPON') {
			$card['general_coupon']=array(
				'base_info'=>$base_info,
				'advanced_info'=>$advanced_info,
				'default_detail'=>$default_detail,
			);
		}else{
			if ($card_type == 'MEMBER_CARD') {
			}
		}
		$jsonData['card']=$card;
		$jsonData=ijson_encode($jsonData);
		$token =  cfc('account')->getAccessToken();		
		$url = 'https://api.weixin.qq.com/card/create?access_token=' . $token;
		$result = cfc('http')->ihttp_post($url, $jsonData);
		$result =@json_decode($result['content'],true);
		return $result;